package com.example.knowledgeget1.ChatController;

import com.example.knowledgeget1.Common.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.example.knowledgeget1.ChatCommon.Entity.NodeType;
import com.example.knowledgeget1.ChatCommon.Service.NodeTypeService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/nodetypes")
public class NodeTypeController {

    private final NodeTypeService nodeTypeService;

    @Autowired
    public NodeTypeController(NodeTypeService nodeTypeService) {
        this.nodeTypeService = nodeTypeService;
    }

    // 获取所有知识点类型
    @GetMapping
    public ResponseEntity<List<NodeType>> getAllNodeTypes() {
        List<NodeType> nodeTypes = nodeTypeService.getAllNodeTypes();
        return new ResponseEntity<>(nodeTypes, HttpStatus.OK);
    }

    //获取所有未构造的知识点
    @GetMapping("/notbuild")
    public ResponseEntity<List<NodeType>> getAllNotBuild(){
    	List<NodeType> notBuildNodes=nodeTypeService.findAllNotBuild();

    	return new ResponseEntity<>(notBuildNodes, HttpStatus.OK);
    }

    // 根据编号获取知识点类型
    @GetMapping("/{id}")
    public ResponseEntity<NodeType> getNodeTypeById(@PathVariable String id) {
        NodeType nodeType = nodeTypeService.getNodeTypeById(id);
        if (nodeType == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(nodeType, HttpStatus.OK);
    }

    // 保存知识点类型
    @PostMapping
    public ResponseEntity<NodeType> saveNodeType(@RequestBody NodeType nodeType) {
        NodeType savedNodeType = nodeTypeService.saveNodeType(nodeType);
        return new ResponseEntity<>(savedNodeType, HttpStatus.CREATED);
    }

    //根据id修改知识点为已构筑
    @PostMapping("/update/build")
    public ResponseEntity<String> updateBuildNodeType(@RequestBody String id) {
        nodeTypeService.updateIsBuildById(id);
        String confirmationMessage = "Node type updated successfully";
        return new ResponseEntity<>(confirmationMessage, HttpStatus.CREATED);
    }

    // 根据id修改知识点为未构筑
    @PostMapping("/update/unbuild")
    public ResponseEntity<String> updateUnBuildNodeType(@RequestBody String id) {
        nodeTypeService.updateIsUnBuildById(id);
        String confirmationMessage = "Node type updated successfully";
        return new ResponseEntity<>(confirmationMessage, HttpStatus.CREATED);
    }

    // 删除知识点类型
    @DeleteMapping("/{name}")
    public ResponseResult deleteNodeType(@PathVariable String name) {
        nodeTypeService.deleteNodeType(name);
        return ResponseResult.success();
    }

    // 根据名称查找知识点类型
    @GetMapping("/byname/{name}")
    public ResponseEntity<NodeType> getNodeTypeByName(@PathVariable String name) {
        NodeType nodeType = nodeTypeService.getNodeTypeByName(name);
        if (nodeType == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(nodeType, HttpStatus.OK);
    }

    // 更新节点
    @PutMapping("/{id}")
    public ResponseEntity<NodeType> updateNodeType(@PathVariable Long id, @RequestBody NodeType nodeType) {
        // 设置节点的id
        nodeType.setId(id);
        NodeType updatedNodeType = nodeTypeService.updateNodeType(nodeType);
        return new ResponseEntity<>(updatedNodeType, HttpStatus.OK);
    }

    // 添加知识点列表到课程
    @PostMapping("/upload")
    public ResponseResult addKnowledgeListToCourse(@RequestBody Map<String, Object> request) {
        Long courseId = Long.parseLong(request.get("course_id").toString());
        List<Map<String, String>> knowledgeList = (List<Map<String, String>>) request.get("knowledgeList");

        List<String> knowledgeNames = knowledgeList.stream()
                .map(map -> map.get("name"))
                .collect(Collectors.toList());

        List<NodeType> existingNodeTypes = nodeTypeService.addNodeTypesFromCourse(courseId, knowledgeNames);

        return ResponseResult.success(existingNodeTypes);
    }

    // 发布知识点
    @PostMapping("/release")
    public ResponseResult addKnowledgeListToCourseRelease(@RequestBody Map<String, Object> request) {
        List<NodeType> failNodeTypes = nodeTypeService.addNodeTypesFromCourseRelease(request);
        return ResponseResult.success(failNodeTypes);
    }
}
